package com.school.teacherServlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.school.teacherServlet.cmd.DisplayGradeManagementFormCommand;
import com.school.teacherServlet.cmd.DisplayMyPayrollFormCommand;
import com.school.teacherServlet.cmd.DisplayReportCardFormCommand;
import com.school.teacherServlet.cmd.DisplayScheduleExamFormCommand;
import com.school.teacherServlet.cmd.DisplayStudentAssignmentFormCommand;
import com.school.teacherServlet.cmd.DisplayStudentAttendanceFormCommand;
import com.school.teacherServlet.cmd.DisplayStudentProfileFormCommand;
import com.school.teacherServlet.cmd.DisplayTeacherTimeTableCommand;
import com.school.teacherServlet.cmd.TeacherCommand;
import com.school.teacherServlet.cmd.TeacherHomeCommand;
import com.school.teacherServlet.cmd.TeacherProfileFormCommand;


/**
 * This servlet will act as teacher controller which will accept request from and send response to teacher only.
 * Servlet implementation class TeacherController
 * @author KK
 */

	public class TeacherController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(TeacherController.class.getSimpleName());
	
	private ServletConfig config;
	   
    private Map<String, TeacherCommand> teacherCommands;
    
    public void init(ServletConfig config) {
		log.info("Entered init(ServletConfig)");
		this.config = config;
		log.info("Cached the ServletConfig object");
		
		teacherCommands = new HashMap<String, TeacherCommand>();
		teacherCommands.put("teacherHome", new TeacherHomeCommand());
		
		teacherCommands.put("displayStudentAttendanceForm", new DisplayStudentAttendanceFormCommand());
		teacherCommands.put("displayTeacherTimeTable", new DisplayTeacherTimeTableCommand());
		teacherCommands.put("displayStudentAssignmentForm", new DisplayStudentAssignmentFormCommand());
		teacherCommands.put("displayStudentProfileForm", new DisplayStudentProfileFormCommand());
		teacherCommands.put("displayScheduleExamForm", new DisplayScheduleExamFormCommand());
		teacherCommands.put("displayGradeManagementForm", new DisplayGradeManagementFormCommand());
		teacherCommands.put("displayReportCardForm", new DisplayReportCardFormCommand());
		teacherCommands.put("displayMyPayrollForm", new DisplayMyPayrollFormCommand());
		teacherCommands.put("teacherProfileForm", new TeacherProfileFormCommand());
		
		
		
		
		
		config.getServletContext().setAttribute("teacherCommands", teacherCommands);
		
		log.info("Exited init(ServletConfig)");
		
		
    }
    
    
    public void init() {
		log.info("Entered init()");
		log.info("Exited init()");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}
	
	
	private void processRequest(HttpServletRequest req, HttpServletResponse resp) 
	throws ServletException, IOException {
		HttpSession session = req.getSession();
		String teacherCommand = req.getParameter("cmd");
		
		System.out.println("Command received is " + teacherCommand);
		
		if (teacherCommands.get(teacherCommand) == null) {
			teacherCommand = "adminHome";
		}
		
		if(session.getAttribute("type")== "teacher"){
			teacherCommands.get(teacherCommand).processRequest(req, resp, config);
		}
		else {
			System.out.println("Acting very smart?? :-):-)!! Relogin to continue!");
		}
		
		
		
		
	}
	

}
